
# -*- coding: UTF-8 -*-

import orjson

from lj_tool import tool_log, tool_try


def lj_json_loads(str):
    try:
        return orjson.loads(str)
    except:
        return str


def dumps(data):
    try:
        if isinstance(data, (str, str, int, float, bool, type(None))):
            return str(data)
        elif isinstance(data, (list, tuple, dict)):
            return orjson.dumps(data, option=orjson.OPT_SORT_KEYS).decode('utf-8')
        elif hasattr(data, '__json__'):
            return data.__json__()
        elif data.__dict__:
            return orjson.dumps(data.__dict__, option=orjson.OPT_SORT_KEYS).decode('utf-8')
        else:
            tool_log.error(f"LJ - 不可序列化对象[{type(data)}][{data}]")
    except:
        tool_log.error(f"LJ - 不可序列化对象[{type(data)}][{data}]")

    return ""
